define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lift/lift',
                    add_url: 'lift/lift/add',
                    edit_url: 'lift/lift/edit',
                    del_url: 'lift/lift/del',
                    multi_url: 'lift/lift/multi',
                    table: 'lift',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'lift_number', title: __('电梯编号'), sortable: true},
                        {field: 'area', title: '地区', operate: 'like'},
                        {field: 'lift_name', title: '梯位信息', operate: 'like'},
                        {field: 'real_name', title: '业主', operate: 'like'},

                        {field: 'phone', title: '业主电话', operate: 'like'},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'examine',
                            title: __('审核状态'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        },
                        {
                            field: 'adv_status', title: __('广告状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('已签约'),
                                "2": __('待签约'),
                            },
                            formatter: function (value, row) {
                                if (row.adv_status == 1) {
                                    return '<span style="color:green">已签约</span>';
                                } else (row.adv_status == 2)
                                {
                                    return '<span style="color:red">待签约</span>';
                                }
                            }
                        },
                        {
                            field: 'adv_add_time',
                            title: __('广告签约时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {field: 'adv_remark', title: '广告备注', operate: false},
                        {field: 'adv_money', title: '签约价格', operate: false},
                        {field: 'adv_date', title: '合同开始时间', operate: false},
                        {field: 'adv_end_date', title: '合同结束时间', operate: false},
                        {
                            field: 'status', title: __('监控状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "2": __('待审核'),
                                "3": __('已审核'),
                            },
                            formatter: function (value, row) {
                                if (row.audit_status == 1 && row.status == 3) {
                                    return '<span style="color:green">已审核</span>';
                                } else if (row.audit_status == 0 && row.status == 3) {
                                    return '<span style="color:red">已审核</span>';
                                } else {
                                    return '待审核';
                                }
                            }
                        },
                        {
                            field: 'contractPayment', title: '合同付款时间', formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'actualPayment', title: '实际付款时间', formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {field: 'paymentCopy', title: '付款凭证', visible: false, operate: false},
                        {field: 'personnel_name', title: '业务员', operate: false},
                        {
                            field: 'add_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'source', title: '创建来源', searchList: {
                                //                                "1": __('待助力'),
                                "1": __('业务员'),
                                "2": __('业主'),
                                "3": __('后台'),
                            },
                            formatter: function (value, row) {
                                if (row.source == 1) {
                                    return '<span style="">业务员</span>';
                                } else if (row.source == 2) {
                                    return '<span style="">业主</span>';
                                } else if (row.source == 3) {
                                    return '<span style="">后台</span>';
                                }
                            }
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'lift/lift/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'change_proprietor',
                                    text: __('更换业主'),
                                    title: __('更换业主'),
                                    classname: 'btn btn-xs btn-warning btn-change btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'lift/lift/change_proprietor?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'assign',
                                    text: __('绑定业务员'),
                                    title: __('绑定业务员'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'lift/lift/do_assign?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                //                                if (row.status == 3) {
                                //                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                //                                } else {
                                //                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                //                                }
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        do_most_assign: function () {
            Controller.api.bindevent();
        },
        //审核
        audit: function () {
            Controller.api.bindevent();
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-0')

            //监听是开票的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },

        content: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'train/train/content',
                    add_url: 'train/train/add_content',
                    edit_url: 'train/train/edit_content',
                    del_url: 'train/train/del_content',
                    multi_url: 'train/train/multi',
                    table: 'content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'train_title', title: '内培标题',},
                        {field: 'train.title', title: '类型名称',},
                        {
                            field: 'train_status', title: __('状态'),
                            searchList: {
                                "1": __('开启'),
                                "0": __('关闭'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 0:
                                        value = '关闭';
                                        break;
                                    case 1:
                                        value = '开启';
                                        break;

                                }
                                return value;
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                //     name: 'city',
                                //     text: __('配置对应内容'),
                                //     title: __('配置对应内容'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                //     icon: 'fa fa-eye',
                                //     url: 'train/train/content?title_id={id}',
                                //     // extend:'data-area=["60%","70%"]',
                                //     callback: function (data) {
                                //     },
                                // },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add_content: function () {
            Controller.api.bindevent();
        },
        edit_content: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser">' + row.useragent.split(" ")[0] + '</a>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="lift/lift/change" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.examine == 1 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
                browser: {
                    'click .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                    }
                },
            }
        }
    };
    return Controller;
});

